Passed
Pull Request — master (#75)
by Mathieu
01:46
created

UpdateEventAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 34
dl 0
loc 36
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 26 2
1
import {
2
  Controller,
3
  Inject,
4
  BadRequestException,
5
  UseGuards,
6
  Param,
7
  Put,
8
  Body
9
} from '@nestjs/common';
10
import {AuthGuard} from '@nestjs/passport';
11
import {ApiUseTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
12
import {EventIdDTO} from './DTO/EventIdDTO';
13
import {LoggedUser} from 'src/Infrastructure/User/Decorator/LoggedUser';
14
import {User} from 'src/Domain/User/User.entity';
15
import {EventDTO} from './DTO/EventDTO';
16
import {ICommandBus} from 'src/Application/ICommandBus';
17
import {UpdateEventCommand} from 'src/Application/FairCalendar/Command/UpdateEventCommand';
18
19
@Controller('events')
20
@ApiUseTags('Event')
21
@ApiBearerAuth()
22
@UseGuards(AuthGuard('bearer'))
23
export class UpdateEventAction {
24
  constructor(
25
    @Inject('ICommandBus')
26
    private readonly commandBus: ICommandBus
27
  ) {}
28
29
  @Put(':id')
30
  @ApiOperation({title: 'Update event'})
31
  public async index(
32
    @Param() idDto: EventIdDTO,
33
    @Body() dto: EventDTO,
34
    @LoggedUser() user: User
35
  ) {
36
    const {type, time, summary, projectId, taskId} = dto;
37
38
    try {
39
      const id = await this.commandBus.execute(
40
        new UpdateEventCommand(
41
          idDto.id,
42
          user,
43
          type,
44
          Number(time),
45
          projectId,
46
          taskId,
47
          summary
48
        )
49
      );
50
51
      return {id};
52
    } catch (e) {
53
      throw new BadRequestException(e.message);
54
    }
55
  }
56
}
57